package com.util;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtil {

    /**
     * 获取指定时间n天后的时间
     * @param dueTime
     * @param n
     * @return
     */
    public static Date getAddDayTime(Date dueTime,int n){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dueTime);
        calendar.add(Calendar.DAY_OF_YEAR, n);
        Date newDate = calendar.getTime();
        return newDate;
    }

    /**
     * 获取下个月第一天或最后一天时间
     * @param type      1：第一天  2：最后一天
     * @return
     */
    public static String getNextMonthStartOrEndTime(int type){
        Calendar calendar = Calendar.getInstance() ;
        calendar.add(Calendar.MONTH , 1);
        calendar.set(Calendar.DAY_OF_MONTH , 1);
        calendar.set(Calendar.HOUR_OF_DAY , 0);
        calendar.set(Calendar.MINUTE , 0);
        calendar.set(Calendar.SECOND , 0);
        Date startTime = calendar.getTime();

        calendar.add(Calendar.MONTH, 1);
        calendar.add(Calendar.MILLISECOND,-1000);
        Date endTime = calendar.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") ;
        if (type == 1){
            return sdf.format(startTime);
        }
        return sdf.format(endTime);
    }

    /**
     * 获取两个时间之间相隔天数
     * @param startTime
     * @param endTime
     * @return
     */
    public static int getDaysApart(Date startTime,Date endTime){
        try {
            Long start = startTime.getTime();
            Long end = endTime.getTime();
            Long num = end-start;//时间戳相差的毫秒数

            Long day = num/24/60/60/1000;
            return (int) Math.round(day);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
}